/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/remove-duplicates-from-sorted-list-ii
   @Language: C++
   @Datetime: 19-07-08 12:55
   */

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
	ListNode* deleteDuplicates(ListNode* head) {
		ListNode H(-1), *p, *q;
		for(H.next=head, p=&H; p && p->next;){
			for(q=p->next; q && q->next && q->val==q->next->val; q=q->next);
			if(p->next==q) p=p->next;
			else p->next=q?q->next:NULL;
		}
		return H.next;
	}
};
